package com.tt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tt.entity.pojo.MovieInfoCommentPO;
import com.tt.entity.pojo.UserPO;
import com.tt.entity.vo.UserCommentVO;
import com.tt.mapper.MovieInfoCommentPOMapper;
import com.tt.mapper.UserPOMapper;
import com.tt.service.MovieInfoCommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * <p>
 * 电影评论表 服务实现类
 * </p>
 *
 * @author TP
 * @since 2023-03-04 09:14:11
 */
@Service
@Deprecated
public class MovieInfoCommentServiceImpl extends ServiceImpl<MovieInfoCommentPOMapper, MovieInfoCommentPO> implements MovieInfoCommentService {

    private final UserPOMapper userPOMapper;
    private final MovieInfoCommentPOMapper movieInfoCommentPOMapper;

    public MovieInfoCommentServiceImpl(UserPOMapper userPOMapper, MovieInfoCommentPOMapper movieInfoCommentPOMapper) {
        this.userPOMapper = userPOMapper;
        this.movieInfoCommentPOMapper = movieInfoCommentPOMapper;
    }

    /**
     * 获取电影评论
     * @param movieId
     * @return
     */
    @Override
    public ArrayList<UserCommentVO> getMovieComments(Integer movieId) {
        // 这里最好其实是联表查, 但是懒得写mapper了
        ArrayList<MovieInfoCommentPO> comments;
        ArrayList<UserPO> users;
        ArrayList<UserCommentVO> userComments = new ArrayList<>();

        comments = new ArrayList<>(movieInfoCommentPOMapper.selectList(new QueryWrapper<MovieInfoCommentPO>().eq("movie_id", movieId)));
        ArrayList<Integer> userIds = new ArrayList<>(comments.size());
        for (MovieInfoCommentPO comment: comments) {
            // 获取userIds
            userIds.add(comment.getUserId());
        }


        users = new ArrayList<>(userPOMapper.selectBatchIds(userIds));
        HashMap<Integer, UserPO> userMap = new HashMap<>();
        for (UserPO user: users) {
            // user转map
            userMap.put(user.getId(), user);
        }

        UserPO tmpUser;
        for (MovieInfoCommentPO comment: comments) {
            tmpUser = userMap.get(comment.getUserId());
            userComments.add(new UserCommentVO(
                    tmpUser.getId(), tmpUser.getName(), tmpUser.getAvatar(), comment.getComment(), comment.getGmtCreated()
                    ));
        }


        return userComments;
    }
}
